package main

import (
	"log"

	"github.com/gin-gonic/gin"

	"github.com/samaa/photosalesplus/backend/internal/cache"
	"github.com/samaa/photosalesplus/backend/internal/config"
	"github.com/samaa/photosalesplus/backend/internal/database"
	"github.com/samaa/photosalesplus/backend/internal/server"
	"github.com/samaa/photosalesplus/backend/internal/storage"
)

func main() {
	cfg, err := config.Load()
	if err != nil {
		log.Fatalf("failed to load config: %v", err)
	}

	if cfg.AppEnv == "production" {
		gin.SetMode(gin.ReleaseMode)
	}

	db, err := database.NewMySQL(cfg)
	if err != nil {
		log.Fatalf("failed to connect to mysql: %v", err)
	}

	redisClient, err := cache.NewRedis(cfg)
	if err != nil {
		log.Fatalf("failed to connect to redis: %v", err)
	}

	if err := storage.EnsureDirectories(cfg); err != nil {
		log.Fatalf("failed to prepare storage directories: %v", err)
	}

	app := server.New(cfg, db, redisClient)

	if err := app.Run(); err != nil {
		log.Fatalf("server stopped with error: %v", err)
	}
}
